home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Librarys / MMULib / C_Sources / MuFastRom.c < prev    next >
C/C++ Source or Header  |  1999-11-28  |  21KB  |  546 lines

  1. /*****************************************************************
  2.  ** MuFastRom                                                   **
  3.  **                                                             **
  4.  ** A MMU-Library compatible ROM remapper                       **
  5.  ** Version 40.8 28.11.1999 © THOR-Software, by Thomas Richter  **
  6.  *****************************************************************/
  7.  
  8. /// Includes
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <exec/ports.h>
  12. #include <dos/dos.h>
  13. #include <mmu/mmubase.h>
  14. #include <mmu/context.h>
  15. #include <mmu/mmutags.h>
  16. #include <workbench/startup.h>
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/mmu.h>
  20. #include <proto/dos.h>
  21. #include <proto/icon.h>
  22. #include <string.h>
  23. ///
  24. /// Defines
  25. #define STRINGDATE "28.11.99"
  26. #define STRINGVERSION "40.8"
  27. #define ROMEND 0x01000000L
  28. #define MAGIC_ROMEND 0x14L
  29. #define CACHEFLAGS (MAPP_CACHEINHIBIT|MAPP_COPYBACK|MAPP_NONSERIALIZED|MAPP_IMPRECISE)
  30. #define TEMPLATE "ON=FASTROM/S,OFF=NOFASTROM/S,HEAD/S,PROTECT/S,NOPROTECT/S"
  31.  
  32. #define OPT_ON  0
  33. #define OPT_OFF 1
  34. #define OPT_HEAD 2
  35. #define OPT_PROTECT 3
  36. #define OPT_NOPROTECT 4
  37. #define OPT_WINDOW 5
  38. #define OPT_COUNT 6
  39. ///
  40. /// Statics
  41. struct MMUBase *MMUBase;
  42. struct DosLibrary *DOSBase;
  43. struct ExecBase *SysBase;
  44. struct Library *IconBase;
  45. ///
  46. /// Protos
  47. int __asm __saveds main(void);
  48. int BuildFastRom(LONG head,LONG protect,LONG noprotect);
  49. int RemoveFastRom(void);
  50. BOOL MapToRom(struct MMUContext *ctx,ULONG source,ULONG mem,ULONG size);
  51. BOOL RemapToRam(struct MMUContext *ctx,ULONG source,ULONG pmem,ULONG mem,ULONG size,LONG protect,LONG noprotect,ULONG cacheflags);
  52. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  53. ///
  54. /// Strukturen
  55. struct FastRomPort {
  56.         struct MsgPort          frp_Port;
  57.         UBYTE                   frp_NoProtect;
  58.         UBYTE                   frp_Protect;
  59.         void                   *frp_Logical;            /* Logical address of the remapped ROM */
  60.         void                   *frp_Physical;           /* Physical location of the RAM used for remapping */
  61.         void                   *frp_RomStart;
  62.         ULONG                   frp_Size;               /* size of the allocated memory */
  63.         struct Library         *frp_Base;               /* Keep the library open */
  64.         ULONG                   frp_CacheFlags;         /* keeps the cache mode of the remapped ROM */
  65.         char                    frp_Name[32];           /* keeps the name of the port */
  66. };
  67. ///
  68.  
  69. char version[]="$VER: MuFastRom " STRINGVERSION " (" STRINGDATE ") © THOR";
  70.  
  71. /// main
  72. int __asm __saveds main(void)
  73. {
  74. LONG args[OPT_COUNT];
  75. struct RDArgs *rd,*myrd;
  76. struct Process *proc;
  77. int rc=20;
  78. LONG err;
  79. struct WBStartup *msg;
  80. BPTR oldout;
  81. struct MsgPort *oldconsole;
  82.  
  83.  
  84.  
  85.         SysBase=*((struct ExecBase **)(4L));
  86.  
  87.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  88.         /* Wait for the workbench startup, if any */
  89.         proc=(struct Process *)FindTask(NULL);
  90.  
  91.         if (!(proc->pr_CLI)) {
  92.                 WaitPort(&(proc->pr_MsgPort));
  93.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  94.         } else  msg=NULL;
  95.  
  96.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  97.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",41L)) {
  98.  
  99.                         err=ERROR_REQUIRED_ARG_MISSING;
  100.  
  101.                         myrd=NULL;      /* reset the temporary ReadArgs */
  102.                         oldout=NULL;
  103.                         oldconsole=NULL;
  104.                         if (msg) {
  105.                                 oldout=SelectOutput(NULL);
  106.                                 oldconsole=SetConsoleTask(NULL);
  107.                                 rd=ReadTTArgs(msg,args,&myrd);
  108.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  109.  
  110.                         if (rd) {
  111.                                 if (!GetMMUType()) {
  112.                                         Printf("MuFastRom requires a working MMU.\n");
  113.                                         err=10;
  114.                                 } else {
  115.                                         /* Argument parser worked, call main routine */
  116.                                         if ((args[OPT_ON]) || (!args[OPT_OFF])) /* ON is the default if OFF is not given */
  117.                                                 err=BuildFastRom(args[OPT_HEAD],args[OPT_PROTECT],args[OPT_NOPROTECT]);
  118.  
  119.                                         if (args[OPT_OFF])
  120.                                                 err=RemoveFastRom();
  121.                                 }
  122.  
  123.                                 FreeArgs(rd);
  124.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  125.                                 if (msg)  Close(SelectOutput(NULL));
  126.                         } else  err=IoErr();
  127.  
  128.                         if (msg) {
  129.                                 SelectOutput(oldout);
  130.                                 SetConsoleTask(oldconsole);
  131.                         }
  132.  
  133.                         if (err<64) {
  134.                                 rc=err;
  135.                                 err=0;
  136.                         } else {
  137.                                 if (!msg) PrintFault(err,"MuFastRom failed");
  138.                                 rc=10;
  139.                         }
  140.                         SetIoErr(err);
  141.  
  142.                         CloseLibrary((struct Library *)MMUBase);
  143.                 } else if (!msg) PrintFault(ERROR_OBJECT_NOT_FOUND,"MuFastRom requires the mmu.library V41 or better");
  144.                 CloseLibrary((struct Library *)DOSBase);
  145.         }
  146.  
  147.         if (msg) {
  148.                 Forbid();
  149.                 ReplyMsg((struct Message *)msg);
  150.         }
  151.  
  152.         return rc;
  153. }
  154. ///
  155. /// ReadTTArgs
  156. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  157. {
  158. struct WBArg *wbarg;
  159. struct DiskObject *dop;
  160. char **tt;                      /* ToolTypes array */
  161. char *wbstr;                    /* Our self-made workbench argument string */
  162. char *here;
  163. BPTR oldlock;
  164. ULONG len;
  165. struct RDArgs *rd=NULL,*myrd=NULL;
  166. LONG err=0;
  167. BPTR newout;
  168.  
  169.         if (IconBase=OpenLibrary("icon.library",37L)) {
  170.                 if (wbarg=msg->sm_ArgList) {
  171.                         /* use a project icon if there is one... */
  172.                         if (msg->sm_NumArgs > 1) wbarg++;
  173.  
  174.                         /* go into the directory */
  175.                         oldlock=CurrentDir(wbarg->wa_Lock);
  176.  
  177.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  178.                                 if (tt=dop->do_ToolTypes) {
  179.                                         /* Read a special tool type for the output window */
  180.  
  181.                                         /* Calc the size of the argument string */
  182.  
  183.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  184.                                         while (*tt) {
  185.                                                 len += strlen(*tt)+1;   /* string, plus space */
  186.                                                 tt++;
  187.                                         }
  188.  
  189.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  190.                                                 /* Now copy the arguments into this string, one by one
  191.                                                    and check whether the argument string is still valid. */
  192.  
  193.                                                 tt=dop->do_ToolTypes;
  194.                                                 here=wbstr;
  195.                                                 do{
  196.                                                         *here='\0';                     /* terminate string */
  197.                                                         /* Check whether this tool type is
  198.                                                            commented out. Just ignore it in this case */
  199.                                                         if (*tt) {
  200.                                                                 if (**tt=='(' || **tt==';')
  201.                                                                         continue;
  202.  
  203.                                                                 strcpy(here,*tt);      /* Add TT string */
  204.